﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Temps;
using PH.Model;
using PH.Logic.Temps;
namespace PH.Web.Management.Areas.Temps.Controllers
{
    [RouteArea("Temps")]
    [RoutePrefix("TBook")]
    public class TBookApiController : ApiController
    {
        [Route("CreateNew")]
        [HttpPost]
        public JsonResult<bool> CreateNew(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                TBookModel model = new TBookModel();
                var btime=request.Value<string>("btime");
                if (!string.IsNullOrEmpty(btime))
                    model.BookTime = DateTime.Parse(btime);
                model.StudentNo = request.Value<string>("studentno");
                model.SchoolName = request.Value<string>("schoolName");
                model.ClassName = request.Value<string>("className");
                model.StudentName = request.Value<string>("studentName");
                #endregion

                TBookLogic.CreateNew(model);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<TBookModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                QueryBase sea = new QueryBase();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                var result = TBookLogic.Gets(sea);
                return new JsonEntitySet<TBookModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<TBookModel>(null, errinfo, false);
        }
    }
}
